use std::any::Any;

use graphics::cosmic_text::Action;

use crate::id::Id;
use crate::message::EventResult;
use crate::sequence::ViewMarker;
use crate::view::{Cx, View};
use crate::widget;
use crate::widget::ChangeFlags;

pub struct Input {}

impl Input {
    pub fn new() -> Self {
        Self {}
    }
}

impl ViewMarker for Input {}

impl<T, A> View<T, A> for Input {
    type State = Option<Action>;
    type Element = widget::Input;

    fn build(&self, cx: &mut Cx) -> (Id, Self::State, Self::Element) {
        let (id, element) = cx.with_new_id(|cx| widget::Input::new(cx.id_path.clone()));
        (id, None, element)
    }

    fn rebuild(
        &self,
        _prev: &Self,
        _cx: &mut Cx,
        _id: &mut Id,
        _state: &mut Self::State,
        _element: &mut Self::Element,
    ) -> ChangeFlags {
        ChangeFlags::LAYOUT | ChangeFlags::PAINT
    }

    fn message(
        &self,
        _id_path: &[Id],
        _state: &mut Self::State,
        event: Box<dyn Any>,
        _app_state: &mut T,
    ) -> EventResult<A> {
        EventResult::Stale(event)
    }
}
